package org.zoomdev.zoom.dao.adapters.result;

import org.zoomdev.zoom.common.caster.Caster;
import org.zoomdev.zoom.common.caster.ValueCaster;
import org.zoomdev.zoom.dao.adapters.ResultSetAdapter;

import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.sql.SQLException;

public class WrapTypeResultSetAdapter implements ResultSetAdapter {

    private final ValueCaster caster;

    public WrapTypeResultSetAdapter(Class<?> columnType, Type fieldType) {
        this.caster = Caster.wrapType(columnType, fieldType);
    }

    @Override
    public Object getObject(ResultSet rs, int columnIndex) throws SQLException {
        Object obj = rs.getObject(columnIndex);
        return caster.to(obj);
    }
}
